﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Web;
using AcctrueWMS.Foundation.DAL;
using System.Data;
using AcctrueWMS.Foundation.Web.Session;

namespace AcctrueWMS.Foundation.Web.Permission
{
    public class ModuleAuthorityManager
    {

        private List<RoleModule> moduleAuthorityList = new List<RoleModule>();

        public void InitModuleAuthority()
        {
            DataTable dt = WMSqlExcuter.ExecuteDataTable("select * from roleModules ");

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                RoleModule module = new RoleModule();
                module.ModuleWBS = dt.Rows[i]["moduleWBS"].ToString();
                module.PrivilegeMask = Convert.ToInt32(dt.Rows[i]["privilegeMask"]);
                module.RoleCode = dt.Rows[i]["roleCode"].ToString();
                moduleAuthorityList.Add(module);
            }

        }
        /// <summary>
        /// 获取权限掩码
        /// </summary>
        /// <param name="roleCode"></param>
        /// <param name="modulewbs"></param>
        /// <returns></returns>
        public List<RoleModule> GetAuthorityMaskCode(string roleCode)
        {
            if (moduleAuthorityList != null)
            {
                return moduleAuthorityList.FindAll(item => item.RoleCode == roleCode);
            }
            else
            {
                return new List<RoleModule>();
            }
        }

        //获取当前用户的module权限信息
        public List<RoleModule> GetCurrentUserModules()
        {
            string currentUserNo = System.Web.HttpContext.Current.Session[SessionConst.UserNo].ToString() == null ? string.Empty : System.Web.HttpContext.Current.Session[SessionConst.UserNo].ToString();
            string currentUserRoleModuleStr = string.Format("select uri.userno,rm.* from dbo.userRoleInfo uri inner join roleModules rm on uri.rolecode=rm.rolecode where uri.userNo='{0}';", currentUserNo);
            DataTable userModule = WMSqlExcuter.ExecuteDataTable(currentUserRoleModuleStr);
            for (int i = 0; i < userModule.Rows.Count; i++)
            {
                RoleModule module = new RoleModule();
                module.ModuleWBS = userModule.Rows[i]["moduleWBS"].ToString();
                module.PrivilegeMask = Convert.ToInt32(userModule.Rows[i]["privilegeMask"]);
                module.RoleCode = userModule.Rows[i]["roleCode"].ToString();
                moduleAuthorityList.Add(module);
            }
            return moduleAuthorityList;
        }

    }

}
